<?php

namespace Dms\Controller;
use Think\Controller;
class AccessController extends Controller{

    public function activityaccess()
    {

    	$activityID = I('get.id');
    	if(empty($activityID) == false)$where['activityID'] = $activityID;
    	$this->activityID = $activityID;

    	$channelID = I('get.channelID',0);
    	if(empty($channelID) == false)$where['sourceID'] = $channelID;
    	$this->channelID = $channelID;

    	$typeArr = I('get.type',array());
    	if(empty($typeArr) == false)$where['type'] = array('IN',implode(',',$typeArr));
    	$this->typeArr = $typeArr;

    	$behaviorArr = I('get.behavior',array());
    	if(empty($behaviorArr) == false)$where['urlType'] = array('IN',implode(',',$behaviorArr));
    	$this->behaviorArr = $behaviorArr;

    	$sourceUrl = I('get.sourceUrl','');
    	if(empty($sourceUrl) == false)$where['sourceUrl'] = array('LIKE',"%$sourceUrl%");
    	$this->sourceUrl = $sourceUrl;

    	$webUrl = I('get.webUrl','');
    	if(empty($webUrl) == false)$where['webUrl'] = array('LIKE',"%$webUrl%");
    	$this->webUrl = $webUrl;

		$startTime       = I('get.startTime');
		$endTime         = I('get.endTime');
    	if(empty($startTime) == true)$startTime = date('Y-m-d');

    	if(empty($endTime) == true )$endTime = date('Y-m-d',time()+24*60*60);

		$this->startTime = $startTime;
		$this->endTime   = $endTime;
    	$where['createTime'] = array(array('egt',strtotime($startTime)),array('elt',strtotime($endTime)));

		$totalJumpNum     = array();
		$totalAccessSpeed = array();
		$totalOrderNum    = array();
		$totalIsUser      = array();
		$accessNum        = array();
		$accesst = array();
		$totalaccess = M2('web_access_log')->where($where)->select();

		foreach ($totalaccess as $key => $value)
		{
			$accessNum[$key] = M2('web_access_log')->where('MATCH(`userKey`) AGAINST("'.$value['userKey'].'")')->count();
			if(empty($value['jumpTime']) == true) continue;
			$totalAccessSpeed[$key] = $value['jumpTime'] - $value['createTime'];
			
		}

		$totaluser = M2('web_access_log')->where($where)->group('userKey')->select();
		// echo M2('web_access_log')->getlastsql();
		foreach ($totaluser as $key => $value)
		{	

			// $t1 = microtime(true);
			$totalOrderNum[$key] = M2('order')->where('MATCH(`authkey`) AGAINST("'.$value['userKey'].'")')->count();
			// $t2 = microtime(true);
			// $accesst[$key] = $t2-$t1;

			// $t1 = microtime(true);
			$member = M2('member')->where(array('anonymous'=>'0','_string'=>'MATCH(`userKey`) AGAINST("'.$value['userKey'].'")'))->find();
			if(empty($member) == false)
			{
				$totalIsUser[$key] = 1;
			}
			// $t2 = microtime(true);
			// $accesst[$key] = $t2-$t1;
		}
		// M3('test')->add(array('content'=>'member:'.array_sum($accesst)));

    	$totalArr = array();
		$totalArr['totalPV']              = count($totalaccess);
		$totalArr['totalUV']              = count($totaluser);
		$totalArr['totalAccessSpeedRate'] = round(array_sum($totalAccessSpeed)/count($totalaccess),2);

		$totalArr['totalOrderNum']        = array_sum($totalOrderNum);
		$totalArr['totalUserNum']         = array_sum($totalIsUser);
		$totalArr['totalUserRate']        = round(array_sum($totalIsUser)/count($totaluser),4)*100;

		$totaljumpNumArr = array_count_values($accessNum);
		$totalArr['totalJumpNumRate']     = round($totaljumpNumArr['1']/count($totalaccess),4)*100;
		$totalArr['totalOrderRate']       = round(array_sum($totalOrderNum)/count($totaluser),4)*100;

    	$this->totalArr = $totalArr;

		$PageClass = new \Think\Page(count($totaluser),10);
		$page      = $PageClass->show();
		$list      = M2('web_access_log')->where($where)->limit($PageClass->firstRow.','.$PageClass->listRows)->group('userKey')->order('createTime ASC')->select();
		// echo M2('web_access_log')->getlastsql();
    	foreach ($list as $key => $value)
    	{

			$list[$key]['createDate']  = date('Y-m-d H:i:s',$value['createTime']);
			$list[$key]['jumpDate']  = empty($value['jumpTime']) ? '' : date('Y-m-d H:i:s',$value['jumpTime']);
			$list[$key]['accessNum']  = M2('web_access_log')->where(array('userKey'=>$value['userKey']))->count();
			$list[$key]['activityTitle'] = M2('activity_list')->where(array('id'=>$value['activityID']))->getField('title');
			$list[$key]['sourceName']    = M2('weixin_qrcode')->where(array('id'=>$value['sourceID']))->getField('title');

			$list[$key]['orderNum']      = M2('order')->where(array('authkey'=>$value['userKey']))->count();

			$member = M2('member')->where(array('userKey'=>$value['userKey'],'anonymous'=>'0','mode'=>'1'))->find();
			if(empty($member) == true)
			{
				$isUser = 0;
			}
			else
			{
				$isUser = 1;
			}
			$list[$key]['isUser'] = $isUser;
    	}
		$this->list = $list;
		$this->page = $page;

    	//活动名称
    	$activitylist = M2('activity_list')->where(array('status'=>array('neq','-1')))->order('id DESC')->select();
    	$this->activitylist = $activitylist;

    	//活动来源
    	$channellist = M2('weixin_qrcode')->where(array('id'=>array('gt','31')))->order('id DESC')->select();
    	$this->channellist = $channellist;

        $this->menuID   = '173';
        $this->menuName = '活动统计';

        $this->display();
    }
}

?>